2000W / 48V LLC Battery-Charger  V1.00
xmc_usic.c
Go to the documentation of this file.
1 
62 /*******************************************************************************
63  * HEADER FILES
64  *******************************************************************************/
65 
66 #include "xmc_usic.h"
67 #include "xmc_scu.h"
68 
69 /*******************************************************************************
70  * MACROS
71  *******************************************************************************/
72 
73 #define USIC_CH_INPR_Msk (0x7UL)
74 
75 /*******************************************************************************
76  * API IMPLEMENTATION
77  *******************************************************************************/
78 
79 void XMC_USIC_CH_Enable(XMC_USIC_CH_t *const channel)
80 {
81  XMC_ASSERT("XMC_USIC_CH_Enable: channel not valid", XMC_USIC_IsChannelValid(channel));
82 
83  if ((channel == XMC_USIC0_CH0) || (channel == XMC_USIC0_CH1))
84  {
86  }
87 #if defined(USIC1)
88  else if((channel == XMC_USIC1_CH0) || (channel == XMC_USIC1_CH1))
89  {
90  XMC_USIC_Enable(XMC_USIC1);
91  }
92 #endif
93 #if defined(USIC2)
94  else if((channel == XMC_USIC2_CH0) || (channel == XMC_USIC2_CH1))
95  {
96  XMC_USIC_Enable(XMC_USIC2);
97  }
98 #endif
99  else
100  {
101  XMC_ASSERT("USIC module not available", 0U/*Always*/);
102  }
103 
104  /* USIC channel switched on*/
106  while ((channel->KSCFG & USIC_CH_KSCFG_MODEN_Msk) == 0U)
107  {
108  /* Wait till the channel is enabled */
109  }
110 
111  /* Set USIC channel in IDLE mode */
112  channel->CCR &= (uint32_t)~USIC_CH_CCR_MODE_Msk;
113 }
114 
115 void XMC_USIC_CH_Disable(XMC_USIC_CH_t *const channel)
116 {
117  channel->KSCFG = (uint32_t)((channel->KSCFG & (~USIC_CH_KSCFG_MODEN_Msk)) | USIC_CH_KSCFG_BPMODEN_Msk);
118 }
119 
120 XMC_USIC_CH_STATUS_t XMC_USIC_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
121 {
122  XMC_USIC_CH_STATUS_t status;
123 
124  uint32_t peripheral_clock;
125 
126  uint32_t clock_divider;
127  uint32_t clock_divider_min;
128 
129  uint32_t pdiv;
130  uint32_t pdiv_int;
131  uint32_t pdiv_int_min;
132 
133  uint32_t pdiv_frac;
134  uint32_t pdiv_frac_min;
135 
136  /* The rate and peripheral clock are divided by 100 to be able to use only 32bit arithmetic */
137  if ((rate >= 100U) && (oversampling != 0U))
138  {
139  peripheral_clock = XMC_SCU_CLOCK_GetPeripheralClockFrequency() / 100U;
140  rate = rate / 100U;
141 
142  clock_divider_min = 1U;
143  pdiv_int_min = 1U;
144  pdiv_frac_min = 0x3ffU;
145 
146  for(clock_divider = 1023U; clock_divider > 0U; --clock_divider)
147  {
148  pdiv = ((peripheral_clock * clock_divider) / (rate * oversampling));
149  pdiv_int = pdiv >> 10U;
150  pdiv_frac = pdiv & 0x3ffU;
151 
152  if ((pdiv_int < 1024U) && (pdiv_frac < pdiv_frac_min))
153  {
154  pdiv_frac_min = pdiv_frac;
155  pdiv_int_min = pdiv_int;
156  clock_divider_min = clock_divider;
157  }
158  }
159 
161  (clock_divider_min << USIC_CH_FDR_STEP_Pos);
162 
163  channel->BRG = (channel->BRG & ~(USIC_CH_BRG_DCTQ_Msk |
167  ((oversampling - 1U) << USIC_CH_BRG_DCTQ_Pos) |
168  ((pdiv_int_min - 1U) << USIC_CH_BRG_PDIV_Pos);
169 
170  status = XMC_USIC_CH_STATUS_OK;
171  }
172  else
173  {
174  status = XMC_USIC_CH_STATUS_ERROR;
175  }
176 
177  return status;
178 }
179 
181  const uint16_t pdiv,
182  const uint32_t oversampling,
183  const XMC_USIC_CH_INPUT_COMBINATION_MODE_t combination_mode)
184 {
185  XMC_ASSERT("XMC_USIC_CH_ConfigExternalInputSignalToBRG: Divider out of range", ((1U < pdiv) || (pdiv < 1024U)));
186  XMC_ASSERT("XMC_USIC_CH_ConfigExternalInputSignalToBRG: Oversampling out of range", ((1U < oversampling) || (oversampling < 32U)));
187 
188  /* Setting the external input frequency source through DX1 */
190 
191  /* Setting the trigger combination mode */
193 
194  /* Configuring the dividers and oversampling */
195  channel->BRG = (channel->BRG & ~(USIC_CH_BRG_DCTQ_Msk |
199  (((oversampling) - 1U) << USIC_CH_BRG_DCTQ_Pos) |
200  (((pdiv) - 1U) << USIC_CH_BRG_PDIV_Pos);
201 }
202 
204  const uint32_t data_pointer,
205  const XMC_USIC_CH_FIFO_SIZE_t size,
206  const uint32_t limit)
207 {
208  /* Disable FIFO */
209  channel->TBCTR &= (uint32_t)~USIC_CH_TBCTR_SIZE_Msk;
210 
211  /* LOF = 0, A standard transmit buffer event occurs when the filling level equals the limit value and gets
212  * lower due to transmission of a data word
213  * STBTEN = 0, the trigger of the standard transmit buffer event is based on the transition of the fill level
214  * from equal to below the limit, not the fact being below
215  */
216  channel->TBCTR = (uint32_t)(channel->TBCTR & (uint32_t)~(USIC_CH_TBCTR_LIMIT_Msk |
219  (uint32_t)((limit << USIC_CH_TBCTR_LIMIT_Pos) |
220  (data_pointer << USIC_CH_TBCTR_DPTR_Pos) |
221  ((uint32_t)size << USIC_CH_TBCTR_SIZE_Pos));
222 }
223 
224 
226  const uint32_t data_pointer,
227  const XMC_USIC_CH_FIFO_SIZE_t size,
228  const uint32_t limit)
229 {
230  /* Disable FIFO */
231  channel->RBCTR &= (uint32_t)~USIC_CH_RBCTR_SIZE_Msk;
232 
233  /* LOF = 1, A standard receive buffer event occurs when the filling level equals the limit value and gets bigger
234  * due to the reception of a new data word
235  */
236  channel->RBCTR = (uint32_t)((channel->RBCTR & (uint32_t)~(USIC_CH_RBCTR_LIMIT_Msk |
239  ((limit << USIC_CH_RBCTR_LIMIT_Pos) |
240  (data_pointer << USIC_CH_RBCTR_DPTR_Pos) |
241  ((uint32_t)size << USIC_CH_RBCTR_SIZE_Pos) |
242  (uint32_t)USIC_CH_RBCTR_LOF_Msk));
243 }
244 
246  const XMC_USIC_CH_FIFO_SIZE_t size,
247  const uint32_t limit)
248 {
249  /* Disable FIFO */
250  channel->TBCTR &= (uint32_t)~USIC_CH_TBCTR_SIZE_Msk;
251 
252  /* STBTEN = 0, the trigger of the standard transmit buffer event is based on the transition of the fill level
253  * from equal to below the limit, not the fact being below
254  */
255  channel->TBCTR = (uint32_t)((uint32_t)(channel->TBCTR & (uint32_t)~USIC_CH_TBCTR_LIMIT_Msk) |
256  (limit << USIC_CH_TBCTR_LIMIT_Pos) |
257  ((uint32_t)size << USIC_CH_TBCTR_SIZE_Pos));
258 }
259 
261  const XMC_USIC_CH_FIFO_SIZE_t size,
262  const uint32_t limit)
263 {
264  /* Disable FIFO */
265  channel->RBCTR &= (uint32_t)~USIC_CH_RBCTR_SIZE_Msk;
266 
267  channel->RBCTR = (uint32_t)((uint32_t)(channel->RBCTR & (uint32_t)~USIC_CH_RBCTR_LIMIT_Msk) |
268  (limit << USIC_CH_RBCTR_LIMIT_Pos) |
269  ((uint32_t)size << USIC_CH_RBCTR_SIZE_Pos));
270 }
271 
273  const XMC_USIC_CH_INTERRUPT_NODE_POINTER_t interrupt_node,
274  const uint32_t service_request)
275 {
276  channel->INPR = (uint32_t)((channel->INPR & (~(uint32_t)(USIC_CH_INPR_Msk << (uint32_t)interrupt_node))) |
277  (service_request << (uint32_t)interrupt_node));
278 }
279 
281  const XMC_USIC_CH_TXFIFO_INTERRUPT_NODE_POINTER_t interrupt_node,
282  const uint32_t service_request)
283 {
284  channel->TBCTR = (uint32_t)((channel->TBCTR & (~(uint32_t)(USIC_CH_INPR_Msk << (uint32_t)interrupt_node))) |
285  (service_request << (uint32_t)interrupt_node));
286 }
287 
289  const XMC_USIC_CH_RXFIFO_INTERRUPT_NODE_POINTER_t interrupt_node,
290  const uint32_t service_request)
291 {
292  channel->RBCTR = (uint32_t)((channel->RBCTR & (~(uint32_t)(USIC_CH_INPR_Msk << (uint32_t)interrupt_node))) |
293  (service_request << (uint32_t)interrupt_node));
294 }
295 
296 void XMC_USIC_Enable(XMC_USIC_t *const usic)
297 {
298  if (usic == USIC0)
299  {
300 #if defined(CLOCK_GATING_SUPPORTED)
301  XMC_SCU_CLOCK_UngatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC0);
302 #endif
303 #if defined(PERIPHERAL_RESET_SUPPORTED)
304  XMC_SCU_RESET_DeassertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC0);
305 #endif
306  }
307 #if defined(USIC1)
308  else if (usic == USIC1)
309  {
310 #if defined(CLOCK_GATING_SUPPORTED)
311  XMC_SCU_CLOCK_UngatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC1);
312 #endif
313 #if defined(PERIPHERAL_RESET_SUPPORTED)
314  XMC_SCU_RESET_DeassertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC1);
315 #endif
316  }
317 #endif
318 #if defined(USIC2)
319  else if (usic == USIC2)
320  {
321 #if defined(CLOCK_GATING_SUPPORTED)
322  XMC_SCU_CLOCK_UngatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC2);
323 #endif
324 #if defined(PERIPHERAL_RESET_SUPPORTED)
325  XMC_SCU_RESET_DeassertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC2);
326 #endif
327  }
328 #endif
329  else
330  {
331  XMC_ASSERT("USIC module not available", 0/*Always*/);
332  }
333 }
334 
335 void XMC_USIC_Disable(XMC_USIC_t *const usic)
336 {
337  if (usic == (XMC_USIC_t *)USIC0)
338  {
339 #if defined(PERIPHERAL_RESET_SUPPORTED)
340  XMC_SCU_RESET_AssertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC0);
341 #endif
342 #if defined(CLOCK_GATING_SUPPORTED)
343  XMC_SCU_CLOCK_GatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC0);
344 #endif
345  }
346 #if defined(USIC1)
347  else if (usic == (XMC_USIC_t *)USIC1)
348  {
349 #if defined(PERIPHERAL_RESET_SUPPORTED)
350  XMC_SCU_RESET_AssertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC1);
351 #endif
352 #if defined(CLOCK_GATING_SUPPORTED)
353  XMC_SCU_CLOCK_GatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC1);
354 #endif
355  }
356 #endif
357 #if defined(USIC2)
358  else if (usic == (XMC_USIC_t *)USIC2)
359  {
360 #if defined(PERIPHERAL_RESET_SUPPORTED)
361  XMC_SCU_RESET_AssertPeripheralReset(XMC_SCU_PERIPHERAL_RESET_USIC2);
362 #endif
363 #if defined(CLOCK_GATING_SUPPORTED)
364  XMC_SCU_CLOCK_GatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_USIC2);
365 #endif
366  }
367 #endif
368  else
369  {
370  XMC_ASSERT("USIC module not available", 0/*Always*/);
371  }
372 
373 }
#define XMC_USIC0_CH1
Definition: xmc_usic.h:130
void XMC_USIC_CH_TXFIFO_Configure(XMC_USIC_CH_t *const channel, const uint32_t data_pointer, const XMC_USIC_CH_FIFO_SIZE_t size, const uint32_t limit)
Definition: xmc_usic.c:203
uint32_t XMC_SCU_CLOCK_GetPeripheralClockFrequency(void)
Definition: xmc1_scu.c:792
void XMC_USIC_CH_SetInterruptNodePointer(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_INTERRUPT_NODE_POINTER_t interrupt_node, const uint32_t service_request)
Definition: xmc_usic.c:272
void XMC_USIC_CH_RXFIFO_SetSizeTriggerLimit(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_FIFO_SIZE_t size, const uint32_t limit)
Definition: xmc_usic.c:260
#define USIC_CH_RBCTR_LIMIT_Pos
Definition: XMC1400.h:3541
void XMC_SCU_CLOCK_UngatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
Definition: xmc1_scu.c:636
#define XMC_USIC0
Definition: xmc_usic.h:128
__STATIC_INLINE bool XMC_USIC_IsChannelValid(const XMC_USIC_CH_t *const channel)
Definition: xmc_usic.h:568
__IO uint32_t KSCFG
Definition: xmc_usic.h:489
#define USIC_CH_BRG_PDIV_Pos
Definition: XMC1400.h:2825
XMC_USIC_CH_STATUS_t XMC_USIC_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
Definition: xmc_usic.c:120
__STATIC_INLINE void XMC_USIC_CH_SetInputTriggerCombinationMode(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_INPUT_t input, const XMC_USIC_CH_INPUT_COMBINATION_MODE_t combination_mode)
Definition: xmc_usic.h:934
__IO uint32_t CCR
Definition: xmc_usic.h:504
#define XMC_USIC0_CH0
Definition: xmc_usic.h:129
#define XMC_ASSERT(msg, exp)
Definition: xmc_common.h:132
void XMC_USIC_CH_TXFIFO_SetSizeTriggerLimit(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_FIFO_SIZE_t size, const uint32_t limit)
Definition: xmc_usic.c:245
enum XMC_USIC_CH_INTERRUPT_NODE_POINTER XMC_USIC_CH_INTERRUPT_NODE_POINTER_t
#define USIC_CH_BRG_PCTQ_Msk
Definition: XMC1400.h:2822
#define USIC_CH_BRG_PPPEN_Msk
Definition: XMC1400.h:2818
#define USIC_CH_TBCTR_LIMIT_Msk
Definition: XMC1400.h:3520
void XMC_USIC_Disable(XMC_USIC_t *const usic)
Definition: xmc_usic.c:335
void XMC_SCU_CLOCK_GatePeripheralClock(const XMC_SCU_PERIPHERAL_CLOCK_t peripheral)
Definition: xmc1_scu.c:628
Universal Serial Interface Controller 0 (USIC)
Definition: XMC1400.h:483
enum XMC_USIC_CH_TXFIFO_INTERRUPT_NODE_POINTER XMC_USIC_CH_TXFIFO_INTERRUPT_NODE_POINTER_t
__STATIC_INLINE void XMC_USIC_CH_SetBRGInputClockSource(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_BRG_CLOCK_SOURCE_t clock_source)
Definition: xmc_usic.h:954
#define USIC_CH_KSCFG_BPMODEN_Msk
Definition: XMC1400.h:2794
void XMC_USIC_CH_RXFIFO_SetInterruptNodePointer(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_RXFIFO_INTERRUPT_NODE_POINTER_t interrupt_node, const uint32_t service_request)
Definition: xmc_usic.c:288
void XMC_USIC_CH_Enable(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.c:79
#define USIC_CH_RBCTR_DPTR_Msk
Definition: XMC1400.h:3540
#define USIC_CH_RBCTR_DPTR_Pos
Definition: XMC1400.h:3539
#define USIC_CH_INPR_Msk
Definition: xmc_usic.c:73
void XMC_USIC_CH_TXFIFO_SetInterruptNodePointer(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_TXFIFO_INTERRUPT_NODE_POINTER_t interrupt_node, const uint32_t service_request)
Definition: xmc_usic.c:280
#define USIC_CH_TBCTR_SIZE_Pos
Definition: XMC1400.h:3529
#define USIC_CH_TBCTR_SIZE_Msk
Definition: XMC1400.h:3530
__IO uint32_t TBCTR
Definition: xmc_usic.h:526
#define USIC_CH_TBCTR_DPTR_Pos
Definition: XMC1400.h:3517
__IO uint32_t RBCTR
Definition: xmc_usic.h:527
#define USIC0
Definition: XMC1400.h:8825
#define USIC1
Definition: XMC1400.h:8826
#define USIC_CH_KSCFG_MODEN_Msk
Definition: XMC1400.h:2792
#define USIC_CH_TBCTR_DPTR_Msk
Definition: XMC1400.h:3518
#define USIC_CH_RBCTR_SIZE_Msk
Definition: XMC1400.h:3554
void XMC_USIC_CH_ConfigExternalInputSignalToBRG(XMC_USIC_CH_t *const channel, const uint16_t pdiv, const uint32_t oversampling, const XMC_USIC_CH_INPUT_COMBINATION_MODE_t combination_mode)
Definition: xmc_usic.c:180
#define USIC_CH_BRG_DCTQ_Pos
Definition: XMC1400.h:2823
#define USIC_CH_CCR_MODE_Msk
Definition: XMC1400.h:3184
void XMC_USIC_CH_Disable(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.c:115
enum XMC_USIC_CH_RXFIFO_INTERRUPT_NODE_POINTER XMC_USIC_CH_RXFIFO_INTERRUPT_NODE_POINTER_t
#define USIC_CH_FDR_STEP_Pos
Definition: XMC1400.h:2805
#define USIC_CH_RBCTR_LIMIT_Msk
Definition: XMC1400.h:3542
#define USIC_CH_TBCTR_LIMIT_Pos
Definition: XMC1400.h:3519
#define USIC_CH_RBCTR_SIZE_Pos
Definition: XMC1400.h:3553
void XMC_USIC_CH_RXFIFO_Configure(XMC_USIC_CH_t *const channel, const uint32_t data_pointer, const XMC_USIC_CH_FIFO_SIZE_t size, const uint32_t limit)
Definition: xmc_usic.c:225
enum XMC_USIC_CH_FIFO_SIZE XMC_USIC_CH_FIFO_SIZE_t
__IO uint32_t INPR
Definition: xmc_usic.h:492
#define USIC_CH_RBCTR_LOF_Msk
Definition: XMC1400.h:3558
#define USIC_CH_BRG_DCTQ_Msk
Definition: XMC1400.h:2824
void XMC_USIC_Enable(XMC_USIC_t *const usic)
Definition: xmc_usic.c:296
#define USIC_CH_BRG_PDIV_Msk
Definition: XMC1400.h:2826
enum XMC_USIC_CH_INPUT_COMBINATION_MODE XMC_USIC_CH_INPUT_COMBINATION_MODE_t
__IO uint32_t FDR
Definition: xmc_usic.h:490
enum XMC_USIC_CH_STATUS XMC_USIC_CH_STATUS_t
__IO uint32_t BRG
Definition: xmc_usic.h:491